define(['jquery'], function($) {
    'use strict';
    //http://192.168.100.15:18080/med/api/subject/list

    $(function() {
        $.get("/list/index/subjectlist", {
            // $.get("/med/subject/list", {
            '__CSRF__': APP.G_csrf
        }).then(function(response) {
            if (response.code == 200) {
                var data = response.message;
                var aChild = data.children;
                if (aChild.length) {
                    /*渲染左侧列表*/
                    var aTrs = [];
                    aChild.forEach(function(n, index) {
                        var sTr = ["<tr data-cid =" + n.cid + "  data-cname =" + n.cname + " class=" + (index == 0 ? 'active' : '') + ">",
                            "       <td>" + n.cname + "</td>",
                            "       <td style='text-align:center'>" + n.c + "</td>",
                            "       <td style='text-align:center'>" + (n.w).toFixed(2) + "</td>",
                            "       <td class='more'>查看</td>",
                            "</tr>"
                        ].join("");
                        aTrs.push(sTr);
                    });
                    var sLeftTabel = ["<table class=\"m-table\">",
                        "  <thead>",
                        "   <tr>",
                        "      <th>名称</th>",
                        "      <th style='text-align:center'>文章数</th>",
                        "      <th style='text-align:center' class=\"cola\">权重</th>",
                        "      <th class=\"colb\">高校排名</th>",
                        "   </tr>",
                        "  </thead>",
                        "  <tbody>" + aTrs.join('') + "</tbody>",
                        "</table>"
                    ].join("");
                    $('.list-left').append(sLeftTabel);
                    //跳转页面链接
                    $('.list-left tbody tr').click(function(e) {
                        var cid = this.getAttribute("data-cid");
                        var cname = this.getAttribute("data-cname");
                        var href = "/index/list?cid=" + cid + '&cname=' + encodeURIComponent(cname);
                        window.open(href);
                    });
                    return aChild;
                }
            }
        }).then(function(response) {
            /*获取学科关键词排行*/
            var getSingleList = function(list, isFirst) {
                var param = {
                    cid: list.cid,
                    '__CSRF__': APP.G_csrf
                };

                /*set title*/
                $('.list-right .title ,title-content').text(list.cname);

                /*第一次加载显示*/
                if (isFirst) {
                    var loding = [
                        " <table class=\"m-table\">",
                        "    <thead>",
                        "        <tr>",
                        "          <th>正在加载中。。。</th>",
                        "        </tr>",
                        "    </thead>",
                        "</table>",
                    ].join("");
                    $('.list-right').append(loding);
                }
                ///list/index/subjectlist
                ///med/subject/list
                ///med/subject/single/list
                $.get("/list/index/singlelist", param, function(res) {
                    //$.get("/list/index/singlelist", function(res) {
                    if (res.code == 200) {
                        var oTable = $(".list-right table");
                        if (oTable[0]) {
                            $(".list-right table").remove();
                            $(".list-right .scroll").remove();
                        };
                        var singleLists = res.message;

                        /*渲染右侧列表*/
                        var aRightTrs = [];
                        if (singleLists.length) {
                            singleLists.forEach(function(n) {
                                //console.log('n', ((n.w) / 100));
                                //96, 134, 2421
                                var color = 'rgba(96, 134, 242, ' + ((n.w) / 10) + ')';
                                var sTr = ["<li style='background:" + color + "'> ",
                                    "<a href='/list/keyword?id=" + n.id + "&cname=" + encodeURIComponent(n.cname) + " ' target='_blank'>" + n.word + "</q>",
                                    "</li>"
                                ].join("");
                                aRightTrs.push(sTr);
                            });
                        } else {
                            var sTr = ["<li class=\"null\">暂时没有数据</li>"].join("");
                            aRightTrs.push(sTr);
                        }
                        var sRightTabel = [
                            " <table class=\"m-table\">",
                            "    <thead>",
                            "        <tr>",
                            "          <th>关键词：</th>",
                            "          <th class='wt_cls'>高 <span>权重</span> 低</th>",
                            "        </tr>",
                            "    </thead>",
                            "</table>",
                            "<ul class=\"scroll\">" + aRightTrs.join('') + "</ul>",
                        ].join("");
                        $('.list-right').append(sRightTabel);
                    }
                }, 'json')
            }

            /*默认显示*/
            getSingleList(response[0], true);
            //var timer = null;

            /*延时显示右侧列表数据*/
            $('.list-left tbody tr').mouseenter(function(e) {
                $(this).addClass('active').siblings().removeClass('active');
                var cid = this.getAttribute("data-cid");
                var cname = this.getAttribute("data-cname");
                // clearTimeout(timer);
                /*timer = setTimeout(function() {
                    getSingleList({
                        cid: cid,
                        cname: cname
                    });
                }, 100);*/
                getSingleList({
                    cid: cid,
                    cname: cname
                });
            });

            /*  $('.list-left tbody tr').mouseout(function(e) {
                  clearTimeout(timer);
              });*/
        });
    });
});
